class CreateEternal < ActiveRecord::Migration
  def self.up
    
    if table_is_here? :pages
      rename_column :pages, :user_id, :friend_id
    else 
      create_table :pages, :force => true do |t|
       t.string   :header,   :limit => 255, :default => "", :null => false
       t.text     :content,  :default => "", :null => false
       t.boolean  :public,   :default => false, :null => false
       t.integer  :friend_id, :default => 0, :null => false
       t.datetime :created_at
      end 
      add_index :pages, ["friend_id"], :name => "fk_pages_friend"
    end
    
    create_table :notes, :force => true do |t|
     t.string   :name,      :limit => 255, :default => "", :null => false
     t.string   :email,     :limit => 100
     t.string   :website
     t.text     :content,   :limit => 357, :default => ""
     t.string   :remote_ip, :limit => 255
     t.integer  :friend_id, :integer, :default => 0
     t.integer  :page_id,   :default => 0, :null => false
     t.datetime :created_at
    end

    create_table :assets, :force => true do |t|
      t.string   :name
      t.text     :content,    :default => ""
      t.boolean  :public,     :default => false, :null => false
      t.integer  :parent_id       # for the thumbnails of resized images, required for plugin
      t.string   :type            # for STI on this table (curent types are image, PDF)
      t.string   :resource_type    # for polymorphism on this table (current types are Program, Question)
      t.integer  :resource_id     # for polymorphism, (program_id, question_id, etc)
      t.string   :content_type     # Fields here [content_type] and below all attachment_fu requirements
      t.string   :filename
      t.string   :thumbnail
      t.integer  :size
      t.integer  :width
      t.integer  :height
      t.datetime :created_at
    end

  end

  def self.down
    drop_table :pages
    drop_table :notes
    drop_table :assets
  end
  
  def self.table_is_here?(name)
    begin
      Friend.connection.execute("select 1 from #{name}")
      say "Checking For Table: #{name} => Table Found"
      return true;
    rescue
      say "Checking For Table: #{name} => Table Not Found"
      return false;
    end
  end
end
